package com.jf.dao.impl;

import com.jf.bean.School;
import com.jf.dao.SchoolDao;
import com.jf.util.BaseDao;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class SchoolDaoImpl implements SchoolDao {
    @Override
    public List<School> getPage(PageInfo pageInfo, School school) {
        List<Object> objs = new ArrayList<>();
        String sql = "select * from school where 1 = 1 ";
        if(MyUtils.isStringNotEmpty(school.getName())){
            sql+="and name = ? ";
            objs.add(school.getName());
        }
        if(school.getStatus() != null){
            sql+="and status = ? ";
            objs.add(school.getStatus());
        }
        sql+="order by id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(School.class,sql,objs);
    }

    @Override
    public int getCount(School school) {
        List<Object> objs = new ArrayList<>();
        String sql = "select count(*) from school where 1 = 1 ";
        if(MyUtils.isStringNotEmpty(school.getName())){
            sql+="and name = ? ";
            objs.add(school.getName());
        }
        if(school.getStatus() != null){
            sql+="and status = ? ";
            objs.add(school.getStatus());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public int addSelect(School school) {
        List<Object> objs = new ArrayList<>();
        String sql = "insert into school(";
        if(school.getId()!=null){
            sql+="id,";
        }
        if(MyUtils.isStringNotEmpty(school.getName())){
            sql+="name,";
        }
        if(MyUtils.isStringNotEmpty(school.getAddress())){
            sql+="address,";
        }
        if(school.getStatus() != null){
            sql+="status,";
        }
        if(school.getEnterpriseId()!=null){
            sql+="enterprise_id,";
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=") values(";
        if(school.getId()!=null){
            sql+="?,";
            objs.add(school.getId());
        }
        if(MyUtils.isStringNotEmpty(school.getName())){
            sql+="?,";
            objs.add(school.getName());
        }
        if(MyUtils.isStringNotEmpty(school.getAddress())){
            sql+="?,";
            objs.add(school.getAddress());
        }
        if(school.getStatus() != null){
            sql+="?,";
            objs.add(school.getStatus());
        }
        if(school.getEnterpriseId()!=null){
            sql+="?,";
            objs.add(school.getEnterpriseId());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=")";
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int updateSelect(School school) {
        List<Object> objs = new ArrayList<>();
        String sql = "update school set ";
        if(MyUtils.isStringNotEmpty(school.getName())){
            sql+="name = ?,";
            objs.add(school.getName());
        }
        if(MyUtils.isStringNotEmpty(school.getAddress())){
            sql+="address = ?,";
            objs.add(school.getAddress());
        }
        if(school.getStatus() != null){
            sql+="status = ?,";
            objs.add(school.getStatus());
        }
        if(school.getEnterpriseId()!=null){
            sql+="enterprise_id = ?,";
            objs.add(school.getEnterpriseId());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=" where id = ?";
        objs.add(school.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int batchDelete(int[] ids) {
        List<Object> objs = new ArrayList<>();
        StringBuilder sql = new StringBuilder("delete from school where id in(");
        for(int id : ids){
            sql.append("?,");
            objs.add(id);
        }
        sql = new StringBuilder(sql.substring(0,sql.lastIndexOf(",")));
        sql.append(")");
        return BaseDao.baseBatch(sql.toString(),ids.length,objs);
    }

    @Override
    public School getById(int id) {
        return BaseDao.baseQueryBean(School.class,"select * from school where id = ?",id);
    }

    @Override
    public List<School> getList() {
        String sql = "select * from school where status = 1 order by id desc";
        return BaseDao.baseQuery(School.class,sql);
    }
}
